//
//  NewsDetailView.m
//  Finance
//
//  Created by cheng xiang on 13-6-30.
//  Copyright (c) 2013年 cheng xiang. All rights reserved.
//

#import "NewsDetailView.h"
#import <QuartzCore/QuartzCore.h>
#import "AnalysisJson.h"
#import "MultiThreading.h"

@implementation NewsDetailView
@synthesize newsId = _newsId;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, -2, self.bounds.size.width, 44)];
        UIImage *root_image = [UIImage imageNamed:@"toolbar.jpg"];
        if ([toolbar respondsToSelector:@selector(setBackgroundImage:forToolbarPosition:barMetrics:)]) {
            [toolbar setBackgroundImage:root_image forToolbarPosition:0 barMetrics:0];         //仅5.0以上版本适用
        }else{
            toolbar.barStyle = UIToolbarPositionTop;
            
        }
        
        UIColor* bk = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"beij.png"]];
        
        UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleBordered target:self action:@selector(goReturn)];
        [leftItem setBackgroundImage:[UIImage imageNamed:@"btn.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
        
        toolbar.items = [NSArray arrayWithObjects:leftItem, nil];
        [self addSubview:toolbar];
        
        self.backgroundColor = [UIColor whiteColor];
        
        //_newsIdLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 150, 20)];
        //_keywordsLabel = [[UILabel alloc] initWithFrame:CGRectMake(160, 0, 150, 20)];
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 44, 300, 60)];
        [_titleLabel setBackgroundColor:[UIColor clearColor]];
        
        _summaryLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 100, 300, 20)];
        [_summaryLabel setBackgroundColor:[UIColor clearColor]];
        
        _titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
        _titleLabel.numberOfLines = 0;
        [_titleLabel setBackgroundColor:[UIColor clearColor]];
        
        _posttimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(55, 110, 200, 20)];
        [_posttimeLabel setBackgroundColor:[UIColor clearColor]];
         [_posttimeLabel setFont:[UIFont fontWithName:@"Arial" size:12]];
        //_readCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(210, 120, 100, 20)];
        //_postUserLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 140, 150, 20)];
        //_postUserNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(160, 140, 150, 20)];
        //_cateIdLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 160, 150, 20)];
        _cateNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(180, 110, 150, 20)];
        [_cateNameLabel setFont:[UIFont fontWithName:@"Arial" size:12]];
        [_cateNameLabel setBackgroundColor:[UIColor clearColor]];
        
        _authorLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 180, 150, 20)];
        _sourceLabel = [[UILabel alloc] initWithFrame:CGRectMake(180, 180, 150, 20)];
//        _contentView = [[UITextView alloc] initWithFrame:CGRectMake(0, 140, 320, 300)];
//        _contentView.editable = NO;
//        [_contentView setBackgroundColor:[UIColor clearColor]];
        
        _contentView  =[[UIWebView alloc] initWithFrame:CGRectMake(0, 140, 320, 300)];
        _contentView.backgroundColor = [UIColor clearColor];
        _contentView.opaque = NO;

//        _imageLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 380, 300, 20)];
//        [_imageLabel setBackgroundColor:[UIColor clearColor]];
        
//        _videoLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 400, 300, 20)];
//        [_videoLabel setBackgroundColor:[UIColor clearColor]];
        
        UIImageView *lineImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 100, self.frame.size.width, 2)];
        lineImageView.image = [UIImage imageNamed:@"line.png"];
        [self addSubview:lineImageView];
        
        //[self addSubview:_newsIdLabel];
        [self addSubview:_titleLabel];
        //[self addSubview:_keywordsLabel];
        [self addSubview:_summaryLabel];
        [self addSubview:_posttimeLabel];
        //[self addSubview:_readCountLabel];
        //[self addSubview:_postUserLabel];
        //[self addSubview:_postUserNameLabel];
        //[self addSubview:_cateIdLabel];
        [self addSubview:_cateNameLabel];
        //[self addSubview:_authorLabel];
        //[self addSubview:_sourceLabel];
        [self addSubview:_contentView];
        [self addSubview:_imageLabel];
        [self addSubview:_videoLabel];
        
         
        [self setBackgroundColor:bk];
    }
    return self;
}

- (void)goReturn
{
    CATransition *transion = [CATransition animation];
    transion.duration = 0.3;
    transion.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    transion.type = kCATransitionFade;
    [self.superview.layer addAnimation:transion forKey:nil];

    [self removeFromSuperview];
}

- (void)getDataForId :(NSString *)newsId
{
    self.newsId = newsId;
    [self packageRequest:@"W114"];
}

- (void)packageRequest: (NSString *)transType
{
    //组请求报文
    NSString *server = @"QueryNewsDetail";
    NSDictionary *header = [AnalysisJson PackageJsonHeader:transType];
    NSDictionary *body  = [self PackageBody:self.newsId];
    NSData *requestJson = [AnalysisJson PackageJsonFromHeader:header andBody:body];
    
    //发送报文
    [self getTableData: requestJson server:server];
}

//每次组报文需要实现类似组body的方法
- (NSDictionary *)PackageBody:(NSString *)newsId
{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic setObject:newsId forKey:@"newsId"];
    return dic;
}

- (void)getTableData:(NSData *)data server:(NSString *)server
{
    [MultiThreading getDataFromNetwork:[SERVICE_URL stringByAppendingString:server] toObject:self callBack:@selector(requestCallBack:) withBody:data];
}

- (void)requestCallBack:(NSDictionary *)dic
{
    NSObject *body = dic[@"body"];
    NSLog(@"%@",body);
    if([body isKindOfClass:[NSDictionary class]] && [(NSDictionary *)body objectForKey:@"news"] != nil)
    {
        NSObject *obj = [(NSDictionary *)body objectForKey:@"news"];
        NSDictionary *dic = [NSDictionary dictionary];
        if ([obj isKindOfClass:[NSDictionary class]]) {
            dic = (NSDictionary *)obj;
        }else if([obj isKindOfClass:[NSArray class]]){
            if ([(NSArray *)obj count] > 0) {
                dic = [(NSArray *)obj objectAtIndex:0];
            }
        }
       // _newsIdLabel.text = [NSString stringWithFormat:@"编号:%@",[dic objectForKey:@"newsId"]];
        _titleLabel.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"title"]];
       // _keywordsLabel.text = [NSString stringWithFormat:@"关键字:%@",[dic objectForKey:@"keywords"]];
        _summaryLabel.text = [dic objectForKey:@"summary"];
        _posttimeLabel.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"posttime"]];
        _readCountLabel.text = [NSString stringWithFormat:@"浏览次数:%@",[dic objectForKey:@"readCount"]];
        _postUserLabel.text = [NSString stringWithFormat:@"发布用户:%@",[dic objectForKey:@"postUser"]];
        _postUserNameLabel.text = [NSString stringWithFormat:@"发布用户名:%@",[dic objectForKey:@"postUserName"]];
        _cateIdLabel.text = [NSString stringWithFormat:@"分类ID:%@",[dic objectForKey:@"cateId"]];
        _cateNameLabel.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"cateName"]];
        _authorLabel.text = [NSString stringWithFormat:@"文章作者:%@",[dic objectForKey:@"author"]];
        _sourceLabel.text = [NSString stringWithFormat:@"来源:%@",[dic objectForKey:@"source"]];
        NSString* strhtmlcontent = [NSString stringWithFormat:@"%@",[dic objectForKey:@"content"]];
        [_contentView loadHTMLString:strhtmlcontent baseURL:nil];
        _imageLabel.text = [NSString stringWithFormat:@"图片链接:%@",[dic objectForKey:@"image"]];
        _videoLabel.text = [NSString stringWithFormat:@"视频链接:%@",[dic objectForKey:@"video"]];
    }
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
